<template>
  <div
    class="tui-conversation"
    @click="handleClickConv"
    @touchstart="handleTouchStart"
    @touchend="handleTouchEnd"
  >
  <view class="bh-row bh-justify-space-between" style="font-size: 24rpx;
    color: #F87147;
    padding: 0 32rpx;
    background-color: rgba(248, 113, 71, 0.1);
    height: 72rpx;
    line-height: 72rpx;">
    温馨提示：系统只保留7天之内的聊天会话哦
    <!-- @click="tips = false" -->
    <!-- <image class="redclose" src="../../static/images/redclose.png" mode="" ></image> -->
  </view>
    <!-- <TUISearch searchType="global" /> -->
    <!-- <ConversationHeader
      v-if="isShowConversationHeader"
      ref="headerRef"
    /> -->
    <ConversationNetwork />
    <ConversationList
      ref="conversationListDomRef"
      class="tui-conversation-list"
      @handleSwitchConversation="handleSwitchConversation"
      @getPassingRef="getPassingRef"
    />
  </div>
</template>
<script lang="ts" setup>
import { TUIStore, StoreName } from '@tencentcloud/chat-uikit-engine';
import { TUIGlobal } from '@tencentcloud/universal-api';
import { ref } from '../../adapter-vue';
import TUISearch from '../TUISearch/index.vue';
import ConversationList from './conversation-list/index.vue';
import ConversationHeader from './conversation-header/index.vue';
import ConversationNetwork from './conversation-network/index.vue';
import { onHide } from '@dcloudio/uni-app'; // 该方法只能用在父组件内，子组件内不生效

import { TUIChatKit, genTestUserSig } from "../../index.ts";
import { vueVersion, onMounted } from "../../adapter-vue";
import { TUILogin } from "@tencentcloud/tui-core";
import { onLoad } from '@dcloudio/uni-app';
import { Api } from '@/api/api';


// #ifdef MP-WEIXIN
// import "./subPackage-init.ts";
// #endif

const emits = defineEmits(['handleSwitchConversation']);

const totalUnreadCount = ref(0);
const headerRef = ref<typeof ConversationHeader>();
const conversationListDomRef = ref<typeof ConversationList>();
const touchX = ref<number>(0);
const touchY = ref<number>(0);
const isShowConversationHeader = ref<boolean>(true);



TUIStore.watch(StoreName.CONV, {
  totalUnreadCount: (count: number) => {
    totalUnreadCount.value = count;
  },
});

TUIStore.watch(StoreName.CUSTOM, {
  isShowConversationHeader: (showStatus: boolean) => {
    isShowConversationHeader.value = showStatus !== false;
  },
});

onLoad((options) => {
	console.log('conversation聊天页面的参数', options.conversationID)
	if(uni.$chat_logined == false){
		console.log('conversationlist loging start')
		TUILogin.login({
		  SDKAppID: uni.$chat_SDKAppID,
		  userID: uni.$chat_userID,
		  // UserSig 是用户登录即时通信 IM 的密码，其本质是对 UserID 等信息加密后得到的密文。
		  // 该方法仅适合本地跑通 Demo 和功能调试，详情请参见 https://cloud.tencent.com/document/product/269/32688     
		  userSig: uni.$chat_userSig, 
		  // 如果您需要发送图片、语音、视频、文件等富媒体消息，请设置为 true
		  useUploadPlugin: true,
		  // 本地审核可识别、处理不安全、不适宜的内容，为您的产品体验和业务安全保驾护航
		  // 此功能为增值服务，请参考：https://cloud.tencent.com/document/product/269/79139
		  // 如果您已购买内容审核服务，开启此功能请设置为 true
		  useProfanityFilterPlugin: false,
		  framework: `vue${vueVersion}` // 当前开发使用框架 vue2 / vue3
		}).then(() => {
			console.log('TUILogin.login success')
			uni.$chat_logined = true;
		}).finally(()=>{
			console.log('conversationlist loging end')
		});
	}else{
		console.log('TUILogin.login has been logined')
	}
	
});

const handleSwitchConversation = (conversationID: string) => {
	console.log('进入聊天会话页面', conversationID)
	let that = this
	// 消息未读通知对方。
	// Api.CommentApi.addImNotify(
	// 	that,
	// 	conversationID.substring(3),
	// 	(res) => {
	// 	},
	// 	false
	// )
	
  TUIGlobal?.navigateTo({
    url: '/TUIKit/components/TUIChat/index?conversationID='+conversationID,
  });
  emits('handleSwitchConversation', conversationID);
};

const closeChildren = () => {
  headerRef?.value?.closeChildren();
  conversationListDomRef?.value?.closeChildren();
};

const handleClickConv = () => {
  closeChildren();
};

onHide(closeChildren);

const handleTouchStart = (e: any) => {
  touchX.value = e.changedTouches[0].clientX;
  touchY.value = e.changedTouches[0].clientY;
};

const handleTouchEnd = (e: any) => {
  const x = e.changedTouches[0].clientX;
  const y = e.changedTouches[0].clientY;
  let turn = '';
  if (x - touchX.value > 50 && Math.abs(y - touchY.value) < 50) {
    // 右滑
    turn = 'right';
  } else if (x - touchX.value < -50 && Math.abs(y - touchY.value) < 50) {
    // 左滑
    turn = 'left';
  }
  if (y - touchY.value > 50 && Math.abs(x - touchX.value) < 50) {
    // 下滑
    turn = 'down';
  } else if (y - touchY.value < -50 && Math.abs(x - touchX.value) < 50) {
    // 上滑
    turn = 'up';
  }
  // 根据方向进行操作
  if (turn === 'down' || turn === 'up') {
    closeChildren();
  }
};

const getPassingRef = (ref) => {
  ref.value = conversationListDomRef.value;
};
</script>

<style lang="scss" scoped src="./style/index.scss">
	.msg {
	  font-size: 24rpx;
	  color: #F87147;
	  padding: 0 32rpx;
	  background-color: rgba(248, 113, 71, 0.1);
	  height: 72rpx;
	  line-height: 72rpx;
	}
</style>
